/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.base;

import java.util.LinkedList;
import java.util.List;

public abstract class AbstractAggregateExtension<Extension>
	implements AggregateExtension<Extension> {

	private final List<Extension> extensions = new LinkedList<Extension>();

	private final Class<Extension> extensionType;

	public AbstractAggregateExtension(Class<Extension> extensionType) {
		this.extensionType = extensionType;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Aggregate methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	public boolean matches(Object potentialExtension) {
		return extensionType().isInstance(potentialExtension);
	}

	public final void addIfMatches(Object potentialExtension) {
		if (matches(potentialExtension)) {
			add(extensionType().cast(potentialExtension));
		}
	}

	public final Class<Extension> extensionType() {
		return extensionType;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Protected methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	protected final Iterable<Extension> extensions() {
		return extensions;
	}

	protected final int numberOfExtensions() {
		return extensions.size();
	}

	protected final Extension getOneExtension() {
		return extensions.get(0);
	}

	protected void add(Extension extension) {
		extensions.add(extension);
	}
}
